package ac.svn.accesscontrol.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RadialGradientPaint;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

public class JButton extends javax.swing.JButton {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private boolean mousePressed = false;

    Color light = new Color(104,204,255);
    Color dark  = new Color(0xA2B5CD); //A2B5CD	
    
    Border border = new MatteBorder(0,0,1,0,Color.LIGHT_GRAY);

    public JButton() {
	setInitialProperties();
    }

    public JButton(Icon arg0) {
	super(arg0);
	setInitialProperties();
    }

    public JButton(String arg0) {
	super(arg0);
	setInitialProperties();
    }

    public JButton(Action arg0) {
	super(arg0);
	setInitialProperties();
    }

    public JButton(String arg0, Icon arg1) {
	super(arg0, arg1);
	setInitialProperties();
    }

    private void setInitialProperties()
    {
	setBorderPainted(false);
	setBorder(border);
	setContentAreaFilled(false);
	addPainter();
    }


    private void addPainter() {
	addMouseListener(new MouseListener() {
	    @Override
	    public void mouseReleased(MouseEvent e) {
		mousePressed = false;
		repaint();
	    }
	    @Override
	    public void mousePressed(MouseEvent e) {
		mousePressed = true;
		repaint();
	    }
	    @Override
	    public void mouseExited(MouseEvent e) {
		repaint();
	    }
	    @Override
	    public void mouseEntered(MouseEvent e) {
		repaint();
	    }
	    @Override
	    public void mouseClicked(MouseEvent e) {
	    }
	});


	addMouseMotionListener(new MouseMotionListener() {
	    @Override
	    public void mouseMoved(MouseEvent e) {
		repaint();
	    }
	    @Override
	    public void mouseDragged(MouseEvent e) {
	    }
	});	
    }


    @Override
    protected void paintComponent(Graphics g) {
	Graphics2D g2 = (Graphics2D) g;

	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

	Paint oldPaint = g2.getPaint();
	if(mousePressed)
	{
	    g2.setColor(Color.GREEN);
	    Paint radical = new RadialGradientPaint(new Point2D.Double(getWidth()/2,getHeight()), getWidth(),
		    new float[] { 0.0f, 1.0f },
		    new Color[] {Color.WHITE,light.brighter().brighter()});
	    g2.setPaint(radical);

	    g2.fill(new RoundRectangle2D.Double(0,0,getWidth(),getHeight(),5,5));

	}

	g2.setPaint(oldPaint);
	super.paintComponent(g);
    }

}
